package com.googlecode.jascii;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;

public class TextReportReader {
	private String encode = null;
	private String[] pageTexts = null;
	private File txtFile;
	private int currentPage = 1;// for 1 .. n
	public TextReportReader(File txtFile ,String encode) throws IOException
	{
		this.txtFile = txtFile;
		this.encode = encode;
		init();
	}
	public TextReportReader(String txtContent ) 
	{
		pageTexts = txtContent.split("\f");
	}
	private void init() throws IOException 
	{
		String txtfile ;
		if(encode == null)
		   txtfile = FileUtils.readFileToString(txtFile);
		else 
		   txtfile = FileUtils.readFileToString(txtFile, encode);
		pageTexts = txtfile.split("\f");
	}
	
	public int getPages()
	{
		return pageTexts.length;
	}
	public int getCurrentPageIndex()
	{
		return currentPage;
	}
	public String getCurrentPage()
	{
		return pageTexts[currentPage-1];
	}
	public String nextPage()
	{
		currentPage++;
		if(currentPage > pageTexts.length) currentPage = pageTexts.length; 
		return pageTexts[currentPage-1];
	}
	public String previousPage()
	{
		currentPage--;
		if(currentPage <= 0) currentPage = 1; 
		return pageTexts[currentPage-1];
	}
	public String getPage(int ipage)
	{
		return pageTexts[ipage];
	}
}
